﻿using Enums;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using XF.Common;
using XF.ExControls;

namespace KYBM
{
    public partial class FrmDTOList : Form
    {
        XF.BLL.Bill_DTOHead bllHead = new XF.BLL.Bill_DTOHead();
        XF.BLL.Bill_DTOItems bll = new XF.BLL.Bill_DTOItems();
        DateTimePicker dtpDateStart = new DateTimePicker();
        DateTimePicker dtpDateEnd = new DateTimePicker();
        int iVerticalScrollIndex = 0;

        public FrmDTOList()
        {
            InitializeComponent();
        }

        private void FrmDTOList_Load(object sender, EventArgs e)
        {
            xfDataGridView1.AutoGenerateColumns = false;
            dtpDateStart.Width = dtpDateEnd.Width = 120; 
            dtpDateStart.Format = dtpDateEnd.Format = DateTimePickerFormat.Custom;
            dtpDateStart.CustomFormat = dtpDateEnd.CustomFormat = "yyyy-MM-dd";
            ToolStripControlHost item = new ToolStripControlHost(dtpDateEnd);
            toolStrip1.Items.Insert(17, item);
            item = new ToolStripControlHost(dtpDateStart);
            toolStrip1.Items.Insert(16, item);

            ColUnit.DisplayMember = MessageText.LISTITEM_TEXT;
            ColUnit.ValueMember = MessageText.LISTITEM_VALUE;
            ColUnit.DataSource = GenerateData.GetUnit(false);
            ColStatus.DisplayMember = MessageText.LISTITEM_TEXT;
            ColStatus.ValueMember = MessageText.LISTITEM_VALUE;
            ColStatus.DataSource = GenerateData.GetDTOStatus(false);
            BindData();
        }

        private void tsBtnSearch_Click(object sender, EventArgs e)
        {
            pagerControl1.PageIndex = 1;
            BindData();
        }

        private void BindData()
        {
            int sum = 0;
            string strWhere = string.Format(" and OrderDate >= '{0}' and OrderDate < '{1}'", dtpDateStart.Value.ToString(MessageText.FORMAT_DATE), dtpDateEnd.Value.AddDays(1).ToString(MessageText.FORMAT_DATE));
            if (!string.IsNullOrEmpty(tsTbCustomer.Text.Trim()))
            {
                strWhere += " and (CustomerCode like '%" + tsTbCustomer.Text.Trim() + "%' or CustomerName like '%" + tsTbCustomer.Text + "%')";
            }
            if (!string.IsNullOrEmpty(tsTbOrderCode.Text.Trim()))
            {
                strWhere += " and OrderCode like '%" + tsTbOrderCode.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(tsTbMaterielName.Text.Trim()))
            {
                strWhere += " and MaterielName like '%" + tsTbMaterielName.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(tsTbMaterielModel.Text.Trim()))
            {
                strWhere += " and MaterielModel like '%" + tsTbMaterielModel.Text.Trim() + "%'";
            }
            List<XF.Model.Bill_DTOItems> models = bll.DataTableToList(bll.GetDetailListByPage(strWhere, " OrderCode Asc ", pagerControl1.PageIndex, pagerControl1.PageSize, ref sum));
            xfDataGridView1.Rows.Clear();
            foreach(XF.Model.Bill_DTOItems model in models)
            {
                int count = xfDataGridView1.RowCount;
                xfDataGridView1.Rows.Add();
                xfDataGridView1.Rows[count].Cells[ColDTOOrderId.Name].Value = model.DTOOrderId;
                xfDataGridView1.Rows[count].Cells[ColOrderCode.Name].Value = model.OrderCode;
                xfDataGridView1.Rows[count].Cells[ColOrderDate.Name].Value = model.OrderDate;
                xfDataGridView1.Rows[count].Cells[ColCreateDate.Name].Value = model.CreateDate;
                xfDataGridView1.Rows[count].Cells[ColCustomerId.Name].Value = model.CustomerId;
                xfDataGridView1.Rows[count].Cells[ColCustomerCode.Name].Value = model.CustomerCode;
                xfDataGridView1.Rows[count].Cells[ColCustomerName.Name].Value = model.CustomerName;
                xfDataGridView1.Rows[count].Cells[ColStatus.Name].Value = model.Status;
                xfDataGridView1.Rows[count].Cells[ColId.Name].Value = model.Id;
                xfDataGridView1.Rows[count].Cells[ColMaterielId.Name].Value = model.MaterielId;
                xfDataGridView1.Rows[count].Cells[ColMaterielName.Name].Value = model.MaterielName;
                xfDataGridView1.Rows[count].Cells[ColMaterielModel.Name].Value = model.MaterielModel;
                xfDataGridView1.Rows[count].Cells[ColUnit.Name].Value = model.Unit;
                xfDataGridView1.Rows[count].Cells[ColQty.Name].Value = model.Qty;
                xfDataGridView1.Rows[count].Cells[ColPrice.Name].Value = model.Price;
                xfDataGridView1.Rows[count].Cells[ColTotle.Name].Value = model.Qty * model.Price;
                xfDataGridView1.Rows[count].Cells[ColOrderRemark.Name].Value = model.OrderRemark;
                xfDataGridView1.Rows[count].Cells[ColReamrk.Name].Value = model.Remark;
                if (model.Status == 3)
                {
                    if (model.PayAmount == 0)
                    {
                        xfDataGridView1.Rows[count].DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#5FAEE3");
                    }
                    else
                    {
                        xfDataGridView1.Rows[count].DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#FF4500");
                    }
                }
                else if (model.Status == 2)
                {
                    xfDataGridView1.Rows[count].DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#32CD32");
                }
                else if (model.Status == 1)
                {
                    xfDataGridView1.Rows[count].DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#FFD700");
                }
                else if (model.Status == 4)
                {
                    xfDataGridView1.Rows[count].DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#DCDCDC");
                }
            }
            pagerControl1.DrawControl(sum);
        }

        private void pagerControl1_OnPageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        private void tsBtnDetail_Click(object sender, EventArgs e)
        {
            List<int> indexs = xfDataGridView1.GetSelectedRowsIndex();
            if (indexs.Count == 1)
            {
                int iId = zDataConverter.ToInt(xfDataGridView1.Rows[indexs[0]].Cells[ColDTOOrderId.Name].Value);
                FrmDTODetail frmDTODetail = new FrmDTODetail(iId);
                if (frmDTODetail.ShowDialog() == DialogResult.OK)
                {
                    BindData();
                    xfDataGridView1.FirstDisplayedScrollingRowIndex = iVerticalScrollIndex;
                }
            }
            else
            {
                QQMessageBox.Show(
                            this,
                            MessageText.TIP_ERROR_SELECTCOUNT_ONE,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
            }
        }

        private void tsBtnPrint_Click(object sender, EventArgs e)
        {
            List<int> indexs = xfDataGridView1.GetSelectedRowsIndex();
            if (indexs.Count > 0)
            {
                List<int> lstId = new List<int>();
                //构建删除ID
                foreach (int i in indexs)
                {
                    int iId = zDataConverter.ToInt(xfDataGridView1.Rows[i].Cells[ColDTOOrderId.Name].Value);
                    if (!lstId.Contains(iId))
                    {
                        lstId.Add(iId);
                        //执行打印
                        XF.Model.Bill_DTOHead head = bllHead.GetDetailModel(iId);
                        head.Items = bll.DataTableToList(bll.GetDetailList(" DTOOrderId = " + iId).Tables[0]);
                        if (bllHead.UpdateStatus(head.OrderCode, 1))
                        {
                            DTOPrinter printer = new DTOPrinter(head);
                            printer.StartPrint();
                        }
                        else
                        {
                            QQMessageBox.Show(
                            this,
                            MessageText.TIP_ERROR_PRINT,
                            MessageText.MESSAGEBOX_CAPTION_ERROR,
                            QQMessageBoxIcon.Error,
                            QQMessageBoxButtons.OK);
                            return;
                        }
                    }
                }
                BindData();
                xfDataGridView1.FirstDisplayedScrollingRowIndex = iVerticalScrollIndex;
            }
            else
            {
                QQMessageBox.Show(
                            this,
                            MessageText.TIP_ERROR_SELECTCOUNT_NULL,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
            }
        }

        private void tsBtnPay_Click(object sender, EventArgs e)
        {
            List<int> indexs = xfDataGridView1.GetSelectedRowsIndex();
            if (indexs.Count == 1)
            {
                string sOrderCode = zDataConverter.ToString(xfDataGridView1.Rows[indexs[0]].Cells[ColOrderCode.Name].Value);
                if(bllHead.UpdateStatus(sOrderCode,3))
                {
                    QQMessageBox.Show(
                            this,
                            MessageText.TIP_SUCCESS_PAY,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
                    BindData();
                    xfDataGridView1.FirstDisplayedScrollingRowIndex = iVerticalScrollIndex;
                }
                else
                {
                    QQMessageBox.Show(
                            this,
                            MessageText.TIP_ERROR_PAY,
                            MessageText.MESSAGEBOX_CAPTION_ERROR,
                            QQMessageBoxIcon.Error,
                            QQMessageBoxButtons.OK);
                }
            }
            else
            {
                QQMessageBox.Show(
                            this,
                            MessageText.TIP_ERROR_SELECTCOUNT_ONE,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
            }
        }

        private void xfDataGridView1_Scroll(object sender, ScrollEventArgs e)
        {
            if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
            {
                iVerticalScrollIndex = e.NewValue;
            }
        }

        private void tsBtnSign_Click(object sender, EventArgs e)
        {
            List<int> indexs = xfDataGridView1.GetSelectedRowsIndex();
            if (indexs.Count == 1)
            {
                string sOrderCode = zDataConverter.ToString(xfDataGridView1.Rows[indexs[0]].Cells[ColOrderCode.Name].Value);
                if (bllHead.UpdateStatus(sOrderCode, 2))
                {
                    QQMessageBox.Show(
                            this,
                            MessageText.TIP_SUCCESS_SIGN,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
                    BindData();
                    xfDataGridView1.FirstDisplayedScrollingRowIndex = iVerticalScrollIndex;
                }
                else
                {
                    QQMessageBox.Show(
                            this,
                            MessageText.TIP_ERROR_SIGN,
                            MessageText.MESSAGEBOX_CAPTION_ERROR,
                            QQMessageBoxIcon.Error,
                            QQMessageBoxButtons.OK);
                }
            }
            else
            {
                QQMessageBox.Show(
                            this,
                            MessageText.TIP_ERROR_SELECTCOUNT_ONE,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
            }
        }

        private void tsBtnCancel_Click(object sender, EventArgs e)
        {
            List<int> indexs = xfDataGridView1.GetSelectedRowsIndex();
            if (indexs.Count == 1)
            {
                FrmDialogRemark frmDialogRemark = new FrmDialogRemark();
                if (frmDialogRemark.ShowDialog() == DialogResult.OK)
                {
                    string sOrderCode = zDataConverter.ToString(xfDataGridView1.Rows[indexs[0]].Cells[ColOrderCode.Name].Value);
                    XF.Model.Bill_DTOHead model = bllHead.GetModel(sOrderCode);
                    model.Enable = false;
                    model.Remark = frmDialogRemark.Remark;
                    if (bllHead.Update(model))
                    {
                        QQMessageBox.Show(
                                this,
                                MessageText.TIP_SUCCESS_CANCEL,
                                MessageText.MESSAGEBOX_CAPTION_TIP,
                                QQMessageBoxIcon.Information,
                                QQMessageBoxButtons.OK);
                        BindData();
                        xfDataGridView1.FirstDisplayedScrollingRowIndex = iVerticalScrollIndex;
                    }
                    else
                    {
                        QQMessageBox.Show(
                                this,
                                MessageText.TIP_ERROR_CANCEL,
                                MessageText.MESSAGEBOX_CAPTION_ERROR,
                                QQMessageBoxIcon.Error,
                                QQMessageBoxButtons.OK);
                    }
                }
            }
            else
            {
                QQMessageBox.Show(
                            this,
                            MessageText.TIP_ERROR_SELECTCOUNT_ONE,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
            }
        }

        private void tsBtnRecovery_Click(object sender, EventArgs e)
        {
            List<int> indexs = xfDataGridView1.GetSelectedRowsIndex();
            if (indexs.Count == 1)
            {
                string sOrderCode = zDataConverter.ToString(xfDataGridView1.Rows[indexs[0]].Cells[ColOrderCode.Name].Value);
                if (bllHead.Enable(sOrderCode))
                {
                    QQMessageBox.Show(
                            this,
                            MessageText.TIP_SUCCESS_RECOVERY,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
                    BindData();
                    xfDataGridView1.FirstDisplayedScrollingRowIndex = iVerticalScrollIndex;
                }
                else
                {
                    QQMessageBox.Show(
                            this,
                            MessageText.TIP_ERROR_RECOVERY,
                            MessageText.MESSAGEBOX_CAPTION_ERROR,
                            QQMessageBoxIcon.Error,
                            QQMessageBoxButtons.OK);
                }
            }
            else
            {
                QQMessageBox.Show(
                            this,
                            MessageText.TIP_ERROR_SELECTCOUNT_ONE,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
            }
        }

        private void FrmDTOList_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F1:
                    tsBtnDetail_Click(this, EventArgs.Empty);
                    break;
                case Keys.F2:
                    tsBtnPrint_Click(this, EventArgs.Empty);
                    break;
                case Keys.F5:
                    tsBtnSign_Click(this, EventArgs.Empty);
                    break;
                case Keys.F6:
                    tsBtnPay_Click(this, EventArgs.Empty);
                    break;
                case Keys.F7:
                    tsBtnRecovery_Click(this, EventArgs.Empty);
                    break;
                case Keys.F8:
                    tsBtnCancel_Click(this, EventArgs.Empty);
                    break;
                        
            }
        }
    }
}
